﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cmsservice.Entity;
using System.Data.SqlClient;

namespace cmsservice.DAL
{
     public class PartnerDAL
    {
         /// <summary>
         /// 获取所有非禁用的合作伙伴,按SortNum倒序排序
         /// </summary>
         /// <returns></returns>
         public List<PartnerType> GetAllandSort()
         {
             string sql = "select a.id,a.name from ( select id,name from newstype where pid =( select id from newstype where name='合作伙伴')) as a " +
                 ",partners where a.id=partners.padr order by partners.sortnum desc";
             return SqlHelper.GetDataByColumnsLst<PartnerType>(DBConn.CMS, sql, null);
         }

         /// <summary>
         /// 根据传过了的type id获取相应的伙伴信息
         /// </summary>
         /// <param name="id"></param>
         /// <returns></returns>
         public PartnerEnty GetOneById(int id)
         {
             Tools tool = new Tools();
             tool.AddSqlParameter("padr", System.Data.SqlDbType.VarChar, id.ToString());
             string sql = "select top 1 * from partners where padr=@padr order by pid desc";
             return SqlHelper.GetDataByColumns<PartnerEnty>(DBConn.CMS, sql, tool.GetSqlParameters());
         }

         /// <summary>
         /// 根据传递的新闻id获取新闻来源的外部链接地址
         /// </summary>
         /// <param name="nid"></param>
         /// <returns></returns>
         public PartnerEnty GetOutUrlByNewsId(string nid)
         {
             Tools tool = new Tools();
             tool.AddSqlParameter("newsid", System.Data.SqlDbType.BigInt, nid);
             //string sql = "select top 1 * from partners where pname in( select name from newstype where id in(select newstypeid from news_type where newsid=@newsid))";
             string sql = "select p.* from partners p left join news_type n on p.padr=n.newstypeid where newsid=@newsid";
             return SqlHelper.GetDataByColumns<PartnerEnty>(DBConn.CMS, sql, tool.GetSqlParameters());
         }
    }
}
